wx.cloud.init({
  env: 'database-2p2zg'
})
const db = wx.cloud.database()
// orderBy方法规定排序
const req = db.collection('school-request').where({
  type: '运动'
}).orderBy('date', 'desc')
Page({
  data: {
    headerImages: [{ imgpath: 'cloud://database-2p2zg.6461-database-2p2zg/project_images/knowledge.png' }, { imgpath: 'cloud://database-2p2zg.6461-database-2p2zg/project_images/sport.png' }, { imgpath: 'cloud://database-2p2zg.6461-database-2p2zg/project_images/knowledge.png' }],
    currentIndex: 0,
    demands: [],
  },
  pageNumber: { skip: 0 },
  getData: function (callback) {
    this.data.openid = wx.getStorageSync('openID')
    wx.showLoading({
      title: '正努力加载数据',
    })
    // res =>{}声明一个函数，传入res作为参数
    req.skip(this.pageNumber.skip).get().then(res => {
      let oldData = this.data.demands
      console.log(oldData.concat(res.data))
      this.setData({ demands: oldData.concat(res.data), openid: this.data.openid })
      this.pageNumber.skip += 20
      res => {
        callback,
          this.pn.skip += 20
      }
      wx.hideLoading()
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData(res => {
      console.log(res.data)
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   * 直接调用数据库get()方法，成功实现了刷新数据的功能
   */
  onPullDownRefresh: function () {
    wx.showLoading({
      title: '正努力加载数据',
    })
    req.get().then(res => {
      this.setData({ demands: res.data })
      wx.hideLoading()
    })
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   * 新建文件，搬代码解决无法触发的问题
   */
  onReachBottom: function () {
    this.getData()
  },

  toMessage: function () {
    var openid = this.data.openid
    wx.navigateTo({
      url: '/pages/message/message' + openid,
    })
  },

  toHead: function () {
    wx.pageScrollTo({
      scrollTop: 0,
    })
  },

  onShareAppMessage: function () {
    return {
      title: '南苑圈子，方便你我',
      path: 'pages/social/social',
    }
  }
})